package com.shop.cereshop.business;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.shop.cereshop.business.service.product.CereShopProductService;
import com.shop.cereshop.business.service.tool.CereShopOperateService;
import com.shop.cereshop.business.timing.OperateInitializationRunnable;
import com.shop.cereshop.business.timing.ProductDownRunnable;
import com.shop.cereshop.business.timing.TimingTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ScheduledFuture;


@SpringBootApplication
@EnableTransactionManagement
@EnableConfigurationProperties
@EnableScheduling
@EnableAutoConfiguration(exclude = {
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@EnableAsync
@EnableDubbo
public class CereshopBusinessApplication extends SpringBootServletInitializer {

    @Autowired
    private static CereShopOperateService cereShopOperateService;

    @Autowired
    private static CereShopProductService cereShopProductService;

    @Autowired
    public void setCereShopProductService(CereShopProductService cereShopProductService) {
        CereshopBusinessApplication.cereShopProductService = cereShopProductService;
    }

    @Autowired
    public void setCereShopOperateService(CereShopOperateService cereShopOperateService) {
        CereshopBusinessApplication.cereShopOperateService = cereShopOperateService;
    }

    /**
     * 需要把web项目打成war包部署到外部tomcat运行时需要改变启动方式
     */
    @Override  //重写configure方法，把springboot的入口给它
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CereshopBusinessApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(CereshopBusinessApplication.class, args);
        //开启运营计划定时任务
        TimingTask timingTask=new TimingTask();
        ScheduledFuture<?> operateFuture = timingTask.startExtensionInitialization("0 0 0 * * ?",
                new OperateInitializationRunnable(cereShopOperateService));
        ScheduledFuture<?> productDown = timingTask.startProductDownInitialization("0 0 0 * * ?",
                new ProductDownRunnable(cereShopProductService));
//        ScheduledFuture<?> operateFuture = timingTask.startExtensionInitialization("0 */1 * * * ?",
//                new OperateInitializationRunnable(cereShopOperateService));

        System.out.println("------   business-server启动成功    ------\n");
    }
    @Bean
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }



}
